package com.happy.cat.project.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.cat.project.system.domain.UserLoginLog;
import com.happy.cat.project.system.mapper.UserLoginLogMapper;
import com.happy.cat.project.system.service.IUserLoginLogService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户登录日志Service业务层处理
 *
 * @author verne
 * @date 2020-08-04
 */
@Service
public class UserLoginLogServiceImpl extends ServiceImpl<UserLoginLogMapper, UserLoginLog> implements IUserLoginLogService {

    @Override
    public Integer getOnlineTimes(Long id) {
        List<UserLoginLog> list = this.lambdaQuery().eq(UserLoginLog::getAccountId, id).list();
        long times = list.stream().mapToLong(e -> {
            if (e.getOfflineTime() != null && e.getCreateTime() != null) {
                return (e.getOfflineTime().getTime() - e.getCreateTime().getTime());
            }
            return 0;
        }).sum();
        if(times>360000){
            return (int)(times/360000);
        }
        return 0;
    }
}
